package com.zwy.alhorthms;

/**
 * 插入排序
 * 从第二个元素开始选取，如果当前的元素比在前面的元素小，则进行交换
 * @author 天火
 * @className InsertionSort
 */
public class InsertionSort extends ArrayData{

    private static InsertionSort insertionSort = new InsertionSort();

    public static void main(String[] args) {
        sort(insertionSort.getData());
        InsertionSort.print(insertionSort.getData());

    }

    static void sort( int[] arr){
        int temp = 0;
        for(int i = 1; i<arr.length; i++){
            temp = arr[i];
            for(int j = i;j>0; j--){
                if(temp<arr[j-1]){
                    arr[j] = arr[j-1];
                    //InsertionSort.swap(arr,j,j-1);
                    if(j-1==0){
                        arr[0] = temp;
                    }
                }else{
                    arr[j] = temp;
                    break;
                }

            }


        }
    }

}
